
package com.jojoteamwork.photo.ui;

import com.jojoteamwork.photo.R;
import com.jojoteamwork.photo.util.ImageWorker;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class PhotoBrowseFragment extends Fragment {
    private static final String IMAGE_PATH_DATA_EXTRA = "image_path";

    private static final String ZIP_DATA_EXTRA = "zip_path";

    private ImageView mImageView;

    private ImageWorker mImageWorker;

    private String mZipPath;

    private String mPhotoPath;

    public static PhotoBrowseFragment newIntance(String zipPath, String photoPath) {
        final PhotoBrowseFragment f = new PhotoBrowseFragment();

        final Bundle args = new Bundle();
        args.putString(IMAGE_PATH_DATA_EXTRA, photoPath);
        args.putString(ZIP_DATA_EXTRA, zipPath);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mZipPath = getArguments().getString(ZIP_DATA_EXTRA);
        mPhotoPath = getArguments().getString(IMAGE_PATH_DATA_EXTRA);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.image_item, null);
        mImageView = (ImageView) v.findViewById(R.id.image);
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        if (PhotoBrowseActivity.class.isInstance(getActivity())) {
            mImageWorker = ((PhotoBrowseActivity) getActivity()).getImageWorker();
            String urls = mZipPath + ";" + mPhotoPath;
            mImageWorker.loadImage(urls, mImageView);
        }
        super.onActivityCreated(savedInstanceState);
    }

    public void cancelWork() {
        ImageWorker.cancelWork(mImageView);
        mImageView.setImageDrawable(null);
        mImageView = null;
    }

}
